/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.fraction;

import me.panpf.sketch.SLog;
import me.panpf.sketch.sample.AppConfig;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.item.SettingsItemProvider;
import me.panpf.sketch.sample.bean.*;
import me.panpf.sketch.sample.util.ScrollingPauseLoadManager;
import me.panpf.sketch.util.Log;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Point;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * SettingsFraction
 */
public class SettingsFraction extends Fraction {
    private ListContainer mRecyclerView;
    private SettingsItemProvider mAdapter;
    private Context mContext;
    private final String[] mLogLevelItems = new String[]{
            "VERBOSE",
            "DEBUG",
            "INFO",
            "WARNING",
            "ERROR",
            "NONE"};

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return scatter.parse(ResourceTable.Layout_settings_fraction, container, false);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mContext = getComponent().getContext();
        mRecyclerView = (ListContainer) getComponent().findComponentById(ResourceTable.Id_list);
        mAdapter = new SettingsItemProvider(mContext, makeMenuList());
        mRecyclerView.setScrollListener(new ScrollingPauseLoadManager(mContext, mAdapter));
        mRecyclerView.setItemProvider(mAdapter);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private List<CommonItem> makeMenuList() {
        List<CommonItem> menuList = new ArrayList<>();

        menuList.add(new MenuTitle("Cache"));
        menuList.add(new CacheInfoMenu(mContext, "Memory", "Memory Cache (Click Clean)"));
        menuList.add(new CacheInfoMenu(mContext, "BitmapPool", "Bitmap Pool (Click Clean)"));
        menuList.add(new CacheInfoMenu(mContext, "Disk", "Disk Cache (Click Clean)"));
        menuList.add(new CheckMenu(mContext, "Disable Memory Cache", AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_MEMORY, null));
        menuList.add(new CheckMenu(mContext, "Disable Bitmap Pool", AppConfig.Key.GLOBAL_DISABLE_BITMAP_POOL, null));
        menuList.add(new CheckMenu(mContext, "Disable Disk Cache", AppConfig.Key.GLOBAL_DISABLE_CACHE_IN_DISK, null));

        menuList.add(new MenuTitle("Gesture Zoom"));
        menuList.add(new CheckMenu(mContext, "Enabled Gesture Zoom In Detail Page", AppConfig.Key.SUPPORT_ZOOM, null));
        menuList.add(new CheckMenu(mContext, "Enabled Read Mode In Detail Page", AppConfig.Key.READ_MODE, null));
        menuList.add(new CheckMenu(mContext, "Enabled Location Animation In Detail Page", AppConfig.Key.LOCATION_ANIMATE, null));
        menuList.add(new CheckMenu(mContext, "Pause Block Display When Page Not Visible In Detail Page", AppConfig.Key.PAUSE_BLOCK_DISPLAY_WHEN_PAGE_NOT_VISIBLE, null));
        menuList.add(new CheckMenu(mContext, "Fixed Three Level Zoom Mode", AppConfig.Key.FIXED_THREE_LEVEL_ZOOM_MODE, null));

        menuList.add(new MenuTitle("GIF"));
        menuList.add(new CheckMenu(mContext, "Auto Play GIF In List", AppConfig.Key.PLAY_GIF_ON_LIST, null));
        menuList.add(new CheckMenu(mContext, "Click Play GIF In List", AppConfig.Key.CLICK_PLAY_GIF, null));
        menuList.add(new CheckMenu(mContext, "Show GIF Flag In List", AppConfig.Key.SHOW_GIF_FLAG, null));

        menuList.add(new MenuTitle("Decode"));
        menuList.add(new CheckMenu(mContext, "In Prefer Quality Over Speed", AppConfig.Key.GLOBAL_IN_PREFER_QUALITY_OVER_SPEED, null));
        menuList.add(new CheckMenu(mContext, "Low Quality Bitmap", AppConfig.Key.GLOBAL_LOW_QUALITY_IMAGE, null));
        menuList.add(new CheckMenu(mContext, "Enabled Thumbnail Mode In List", AppConfig.Key.THUMBNAIL_MODE, null));
        menuList.add(new CheckMenu(mContext, "Cache Processed Image In Disk", AppConfig.Key.CACHE_PROCESSED_IMAGE, null));
        menuList.add(new CheckMenu(mContext, "Disabled Correct Image Orientation", AppConfig.Key.DISABLE_CORRECT_IMAGE_ORIENTATION, null));

        menuList.add(new MenuTitle("Other"));
        menuList.add(new CheckMenu(mContext, "Show Round Rect In Photo List", AppConfig.Key.SHOW_ROUND_RECT_IN_PHOTO_LIST, null));
        menuList.add(new CheckMenu(mContext, "Show Unsplash Raw Image In Detail Page", AppConfig.Key.SHOW_UNSPLASH_RAW_IMAGE, null));
        menuList.add(new CheckMenu(mContext, "Show Mapping Thumbnail In Detail Page", AppConfig.Key.SHOW_TOOLS_IN_IMAGE_DETAIL, null));
        menuList.add(new CheckMenu(mContext, "Show Press Status In List", AppConfig.Key.CLICK_SHOW_PRESSED_STATUS, null));
        menuList.add(new CheckMenu(mContext, "Show Image From Corner Mark", AppConfig.Key.SHOW_IMAGE_FROM_FLAG, null));
        menuList.add(new CheckMenu(mContext, "Show Download Progress In List", AppConfig.Key.SHOW_IMAGE_DOWNLOAD_PROGRESS, null));
        menuList.add(new CheckMenu(mContext, "Click Show Image On Pause Download In List", AppConfig.Key.CLICK_RETRY_ON_PAUSE_DOWNLOAD, null));
        menuList.add(new CheckMenu(mContext, "Click Retry On Error In List", AppConfig.Key.CLICK_RETRY_ON_FAILED, null));
        menuList.add(new CheckMenu(mContext, "Scrolling Pause Load Image In List", AppConfig.Key.SCROLLING_PAUSE_LOAD, null));
        menuList.add(new CheckMenu(mContext, "Mobile Data Pause Download Image", AppConfig.Key.MOBILE_NETWORK_PAUSE_DOWNLOAD, null));
        menuList.add(new CheckMenu(mContext, "Long Clock Show Image Info", AppConfig.Key.LONG_CLICK_SHOW_IMAGE_INFO, null));

        menuList.add(new MenuTitle("Log"));
        menuList.add(logInfoMenu);
        menuList.add(new CheckMenu(mContext, "Output Flow Log", AppConfig.Key.LOG_REQUEST, null));
        menuList.add(new CheckMenu(mContext, "Output Cache Log", AppConfig.Key.LOG_CACHE, null));
        menuList.add(new CheckMenu(mContext, "Output Zoom Log", AppConfig.Key.LOG_ZOOM, null));
        menuList.add(new CheckMenu(mContext, "Output Zoom Block Display Log", AppConfig.Key.LOG_ZOOM_BLOCK_DISPLAY, null));
        menuList.add(new CheckMenu(mContext, "Output Used Time Log", AppConfig.Key.LOG_TIME, null));
        menuList.add(new CheckMenu(mContext, "Sync Output Log To Disk (cache/sketch_log)", AppConfig.Key.OUT_LOG_2_SDCARD, null));

        return menuList;
    }

    private final InfoMenu logInfoMenu = new InfoMenu("Log Level") {
        @Override
        public String getInfo() {
            int level = SLog.getLevel();
            switch (level) {
                case SLog.LEVEL_VERBOSE: return "VERBOSE";
                case SLog.LEVEL_DEBUG: return "DEBUG";
                case SLog.LEVEL_INFO: return "INFO";
                case SLog.LEVEL_WARNING: return "WARNING";
                case SLog.LEVEL_ERROR: return "ERROR";
                case SLog.LEVEL_NONE: return "NONE";
                default: return "Unknown";
            }
        }

        @Override
        public void onClick(BaseItemProvider provider) {
            CommonDialog dialog = new CommonDialog(mContext);

            Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_settings_fraction_log_info, null, false);
            DirectionalLayout listLayout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_list_layout);

            for (String logLevelItem : mLogLevelItems) {
                Component item = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_list_item_log_info, null, false);
                Text textView = (Text) item.findComponentById(ResourceTable.Id_text);
                if (logInfoMenu.getInfo().equals(logLevelItem)) {
                    textView.setText(logLevelItem + " (*)");
                } else {
                    textView.setText(logLevelItem);
                }
                textView.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (!textView.getText().contains("(*)")) {
                            AppConfig.putString(mContext, AppConfig.Key.LOG_LEVEL, textView.getText());

                            if (provider instanceof SettingsItemProvider) {
                                SettingsItemProvider settingsItemProvider = (SettingsItemProvider) provider;
                                int position = settingsItemProvider.getList().indexOf(logInfoMenu);
                                settingsItemProvider.notifyDataSetItemChanged(position);
                            }
                        }
                        if (dialog.isShowing()) {
                            dialog.hide();
                        }
                    }
                });
                listLayout.addComponent(item);
            }

            component.findComponentById(ResourceTable.Id_bt_cancel).setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (dialog.isShowing()) {
                        dialog.hide();
                    }
                }
            });

            Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(mContext);
            Point pt = new Point();
            display.get().getSize(pt);

            dialog.setSize((int) (pt.getPointX() * 0.85f), ComponentContainer.LayoutConfig.MATCH_CONTENT);
            dialog.setContentCustomComponent(component);
            dialog.show();
        }
    };

}
